using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace MosziNET.Web.Gallery.Providers
{
    /// <summary>
    /// Helper methods on the site map provider
    /// </summary>
    public static class GallerySiteMapPathProvider
    {
        /// <summary>
        /// Fills the site map node with gallery information
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="parentFolder"></param>
        public static int GetNodes(SiteMapNode parentNode, String parentFolder, 
            SiteMapProvider provider, List<SiteMapNode> childList, List<SiteMapNode> parentList)
        {
            Folder[] folderList = GalleryManager.StorageProvider.GetFolderList(parentFolder);
            for (int i = 0; i < folderList.Length; i++)
            {
                string path = GalleryManager.GetFolderUrl(folderList[i].Id);
                path = path.Replace("~/", ApplicationPath);
                // create and add the found node
                SiteMapNode newParent = new SiteMapNode(provider,
                    Guid.NewGuid().ToString(), path,
                    folderList[i].Title);

                // add the 2 nodes ...
                childList.Add(newParent);
                parentList.Add(parentNode);
                
                // now fill the children of this node as well
                int addedCount = GetNodes(newParent, folderList[i].Id, provider, childList, parentList);
                if (addedCount == 0)
                {
                    SiteMapNode newFake = new SiteMapNode(provider,
                        Guid.NewGuid().ToString(), path + ".image",
                        "(image)");

                    childList.Add(newFake);
                    parentList.Add(newParent);
                }
            }

            return folderList.Length;
        }

        /// <summary>
        /// Tests if the provided url is a picture URL
        /// </summary>
        /// <param name="folderId"></param>
        /// <returns></returns>
        public static string GetFakeImageUrl(string folderId)
        {
            return GalleryManager.GetFolderUrl(
                        folderId) + ".image";
        }

        /// <summary>
        /// Cuts the heading application path if any 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static string GetRelativePath(string path)
        {
            if (path.IndexOf(ApplicationPath) != 0)
                return path;
            else
                return "~/" + path.Substring(ApplicationPath.Length);
        }

        /// <summary>
        /// Returns the corrected application path
        /// </summary>
        private static string ApplicationPath
        {
            get
            {
                string appPath = HttpContext.Current.Request.ApplicationPath;
                if (!appPath.EndsWith("/"))
                {
                    appPath += "/";
                }

                return appPath;
            }
        }
    }
}
